package com.siyoumi.app.modules.sms.admin;

import com.siyoumi.app.entity.SysSms;
import com.siyoumi.app.entity.SysStock;
import com.siyoumi.app.modules.sms.service.SvcSms;
import com.siyoumi.app.modules.sms.vo.VaSms;
import com.siyoumi.app.modules.sms.vo.VoSmsSendTest;
import com.siyoumi.app.service.SysStockService;
import com.siyoumi.app.service.SysStoreService;
import com.siyoumi.app.sys.service.ApiHandle;
import com.siyoumi.app.sys.service.api_handle.vo.VoApiSmsSend;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/sms/sms__edit")
public class sms__edit
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("短信-编辑");

        Map<String, Object> data = new HashMap<>();
        Long leftCount = 0L;
        Long leftCountOld = 0L;
        if (isAdminEdit()) {
            SysSms entity = SvcSms.getApp().loadEntity(getID());
            //
            SysStock entityStock = SysStockService.getBean().getEntityBySrc(entity.getKey(), false);
            if (entityStock != null) {
                leftCount = entityStock.getStock_count_left();
                leftCountOld = entityStock.getStock_count_left();
            }
            //
            HashMap<String, Object> dataAppend = new LinkedHashMap<>();
            dataAppend.put("id", entity.getKey());
            //
            //合并
            data = entity.toMap();
            data.putAll(dataAppend);
        }
        data.put("left_count", leftCount);
        data.put("left_count_old", leftCountOld);
        data.put("set_left", 0);
        getR().setData("data", data);
        //
        return getR();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/save")
    public XReturn save(@Validated() VaSms vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        //
        {
            JoinWrapperPlus<SysSms> query = SvcSms.getApp().join();
            query.eq("sms_template_code", vo.getSms_template_code());
            if (isAdminEdit()) {
                query.ne("sms_id", getID());
            }
            SysSms entity = SvcSms.getApp().first(query);
            if (entity != null) {
                result.addError(XValidator.getErr("sms_template_code", "短信模板Code已存在"));
            }
        }
        XValidator.getResult(result);
        //
        vo.setSms_acc_id(getAccId());
        //
        InputData inputData = InputData.fromRequest();
        return SvcSms.getBean().edit(inputData, vo);
    }

    //测试发送
    @PostMapping("/test_send")
    public XReturn testSend(@Validated @RequestBody VoSmsSendTest vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result, true);
        //
        SysSms entitySms = SvcSms.getApp().getEntity(vo.getSms_id());
        XValidator.isNull(entitySms, "sms_id error");
        //
        ApiHandle api = ApiHandle.of(entitySms.getSms_api_type());
        //
        VoApiSmsSend apiData = new VoApiSmsSend();
        apiData.setPhone(vo.getTest_phone());
        apiData.setSign_name(entitySms.getSms_sign_name());
        apiData.setTemplate_code(entitySms.getSms_template_code());
        apiData.setTemplate_json(vo.getTemplate_json());
        return api.smsSend(apiData);
    }
}